package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.ParcelBean;
import com.shouhuobao.take.utils.StringUtils;

import droid.frame.activity.title.TitleRes;
import droid.frame.ui.dialog.BaseDialog;

/**
 * 包裹详情
 *
 * @author coffee<br>
 * 2017年6月5日下午6:25:31
 */
public class ParcelDetailsActivity extends BaseActivity2 implements View.OnClickListener {

    private TextView mExpressCompanyName, mExpressNumber;
    private TextView mStorageCode;
    private TextView mReceiverPhone, mReceiverName;
    private TextView mRemark;
    private TextView mTakeTime, mSignTime;
    private TextView mExceptionDesc;

    private BaseDialog mDialog;
    private ParcelBean item;

    @Override
    protected void findViewById() {
        setContentView(R.layout.take_parcel_detail);
        super.findViewById();
        item = (ParcelBean) getIntent().getSerializableExtra("item");
        if (item.isInput()) {
            getAppTitle().setCommonTitle("包裹详情", new TitleRes("异常申报", new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), ParcelExceptionActivity.class);
                    intent.putExtra("item", item);
                    startActivityForResult(intent, 104);
                }
            }));
        } else {
            getAppTitle().setCommonTitle("包裹详情");
        }
        mExpressCompanyName = (TextView) findViewById(R.id.parcel_express_companyName);
        mExpressNumber = (TextView) findViewById(R.id.parcel_express_number);
        mStorageCode = (TextView) findViewById(R.id.parcel_code);
        mReceiverPhone = (TextView) findViewById(R.id.parcel_receiver_phone);
        mReceiverName = (TextView) findViewById(R.id.parcel_receiver_name);
        mRemark = (TextView) findViewById(R.id.parcel_remark);
        this.mTakeTime = (TextView) findViewById(R.id.parcel_takeaway_time);
        this.mSignTime = (TextView) findViewById(R.id.parcel_sign_time);
        this.mExceptionDesc = (TextView) findViewById(R.id.parcel_exception_desc);

        findViewById(R.id.submit).setOnClickListener(this);
        findViewById(R.id.submit_2).setOnClickListener(this);
        findViewById(R.id.submit_3).setOnClickListener(this);
        //
        setParcelData(item);
        // 去服务器查询
        AppHttp.getInstance().takeParcelDetails(item.getId());
    }

    private void setParcelData(ParcelBean parcel) {
        if (parcel == null) {
            return;
        }
        mExpressCompanyName.setText(parcel.getExpressCompanyName() + ": ");
        mExpressNumber.setText(StringUtils.splitNumString(2, parcel.getExpressNumber()));
        mStorageCode.setText(getResources().getString(R.string.take_bag_name_desc) + "：" + parcel.getStorageCode());
        mReceiverPhone.setText(parcel.getReceiverPhone());
        //
        mReceiverName.setText(parcel.getReceiverName());
        mRemark.setText(parcel.getRemark());
        //已超期
        if (item.isOthers()) {
            mTakeTime.setText("超期日期: " + parcel.getTakeTime());
        } else {
            mTakeTime.setText("取走日期: " + parcel.getTakeTime());
        }
        mSignTime.setText(parcel.getCreateTime());
        mExceptionDesc.setText(parcel.getExceptionDesc());

        if (parcel.isInput() || parcel.getStatus() == 0) {
            ((ViewGroup) mExceptionDesc.getParent()).setVisibility(View.GONE);
            ((ViewGroup) mTakeTime.getParent()).setVisibility(View.GONE);
            findViewById(R.id.submit_layout).setVisibility(View.VISIBLE);
        } else {
            ((ViewGroup) mExceptionDesc.getParent()).setVisibility(View.VISIBLE);
            ((ViewGroup) mTakeTime.getParent()).setVisibility(View.VISIBLE);
            findViewById(R.id.submit_layout).setVisibility(View.GONE);
        }
        mReceiverPhone.setOnClickListener(this);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.take_sms_notice:
                cancelLoadingDialog();
                ReqResult<?> result2 = JSON.parser(msg.obj);
                if (checkLoginStatus(result2)) {

                    showToast(result2.getMessage());
                } else {
                    showToast(result2.getMessage());
                }
                return false;
            // 确认已取走
            case MsgID.take_parcel_away:
                cancelLoadingDialog();
                ReqResult<?> result3 = JSON.parser(msg.obj);
                if (checkLoginStatus(result3)) {
                    showToast(result3.getMessage());
                } else {
                    showToast(result3.getMessage());
                }
                return false;
            // 取走
            case MsgID.take_parcel_check_bycheckcode:
                cancelLoadingDialog();
                ReqResult<?> result1 = JSON.parser(msg.obj);
                if (checkLoginStatus(result1)) {
                    showToast(result1.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mDialog != null) {
                                mDialog.cancel();
                            }
                            Intent intent = new Intent();
                            setResult(Activity.RESULT_OK, intent);
                            finish();
                        }
                    });
                } else {
                    showToast(result1.getMessage());
                }
                return false;
            case MsgID.take_parcel_details:
                cancelLoadingDialog();
                final ReqResult<ParcelBean> result = JSON.parser(msg.obj, ParcelBean.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            setParcelData(result.getResult());
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            //
            case R.id.parcel_receiver_phone:
                intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                String phone = mReceiverPhone.getText().toString().replace(" ", "");
                intent.setData(Uri.parse("tel:" + phone));
                startActivity(intent);
                break;
            //密码取货
            case R.id.submit:
                // showLoadingDialog(null);
                // AppHttp.getInstance().takeParcelAway(item.getId(), null);
                mDialog = DialogMgr.showTakeAwayByCode(this, item);
                break;
            case R.id.submit_2:
                showLoadingDialog(null);
                AppHttp.getInstance().takeSmsNotice(item.getId());
                break;
            case R.id.submit_3:
                intent = new Intent(getContext(), ParcelConfirmTakeActivity.class);
                intent.putExtra("item", item);
                startActivityForResult(intent, 104);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 104) {
            setResult(Activity.RESULT_OK);
            finish();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
